package com.chs.recoginition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chs.recoginition.mapper.GiveUpMapper;
import com.chs.recoginition.pojo.GiveUp;
import com.chs.recoginition.pojo.IdentificationContent;
import com.chs.recoginition.pojo.InvitationContent;
import com.chs.recoginition.service.IGiveUpService;
import com.chs.recoginition.service.IInvitationContentService;
import com.chs.recoginition.vo.PageRespVo;
import com.chs.recoginition.vo.reqvo.GiveUpPageVo;
import com.chs.recoginition.vo.respVo.GiveUpRespVo;
import com.chs.recoginition.vo.respVo.InvitationContentRespVo;
import com.chs.recoginition.vo.respVo.UserRespVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 * @author CHS
 * @since 2023-04-10
 */
@Service
public class GiveUpServiceImpl extends ServiceImpl<GiveUpMapper, GiveUp> implements IGiveUpService {
    @Resource
    private IInvitationContentService invitationContentService;

    @Override
    public PageRespVo<GiveUpRespVo> findPage(GiveUpPageVo reqVo) {
        PageRespVo<GiveUpRespVo> result = baseMapper.findPage(reqVo);
        for(int i=0;i<result.getRows().size();i++) {
            Integer id = result.getRows().get(i).getInvitationId();
            InvitationContentRespVo byId = invitationContentService.findById(id);
            result.getRows().get(i).setRespVo(byId);
        }
        return result;
    }

    @Override
    public void save(GiveUp vo, UserRespVo user) {
        vo.setCreatedBy(user.getUserId());
        vo.setCreatedTime(LocalDateTime.now());
        baseMapper.insert(vo);
    }

    @Override
    public Boolean getBoolean(Integer invitationId, UserRespVo user) {
        boolean result = false;
        QueryWrapper<GiveUp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("INVITATION_ID",invitationId);
        queryWrapper.eq("CREATED_BY",user.getUserId());
        List<GiveUp> giveUps = baseMapper.selectList(queryWrapper);
        if(!giveUps.isEmpty()) {
            result = true;
        }
        return result;
    }

    @Override
    public Integer getParse(Integer invitationId, UserRespVo user) {
            QueryWrapper<GiveUp> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("INVITATION_ID",invitationId);
            queryWrapper.eq("CREATED_BY",user.getUserId());
            List<GiveUp> giveUps = baseMapper.selectList(queryWrapper);
            if(!giveUps.isEmpty()) {
                List<Integer> collect = giveUps.stream().map(v -> v.getId()).collect(Collectors.toList());
                baseMapper.deleteBatchIds(collect);
            }else {
                GiveUp giveUp = new GiveUp();
                giveUp.setInvitationId(invitationId);
                giveUp.setCreatedBy(user.getUserId());
                giveUp.setCreatedTime(LocalDateTime.now());
                baseMapper.insert(giveUp);
            }
            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("INVITATION_ID",invitationId);
            List<GiveUp> list = baseMapper.selectList(queryWrapper);
            return list.size();
    }

    @Override
    public Integer getSize(Integer invitationId) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("INVITATION_ID",invitationId);
        List<GiveUp> list = baseMapper.selectList(queryWrapper);
        return list.size();
    }

    @Override
    public void remove(String idList) {
        List<Integer> ids = Arrays.stream(idList.split(",")).map(Integer::parseInt).collect(Collectors.toList());
        baseMapper.deleteBatchIds(ids);
    }
}
